<?php


namespace app\admin\controller;


use app\admin\common\controller\Base;
use app\admin\common\model\Cate as CateModel;
use think\facade\Request;

class Cate extends Base
{
    public function index(){
        $this->isLogin();
        return $this->redirect('cateList');
    }


    //分类列表
    public function cateList(){
        //判断是否登录了
        $this->isLogin();
        //查找出说有的栏目
        $cate=CateModel::all();
        if(!is_null($cate)){
            $this->assign('cateList',$cate);
        }
        $this->assign('empty','没有任何栏目');
        //设置标题
        $this->assign('title','分类列表');
        //渲染
        return $this->fetch();
    }

    //添加分类
    public function cateAdd(){
        if(Request::isPost()){
            $data=Request::post();
            CateModel::create($data);
            $this->success('添加分类成功','cate/catelist');
        }
        //设置标题
        $this->assign('title','添加分类');
        //渲染
        return $this->fetch();
    }


    //分类编辑界面
    public function cateEdit(){
        //判断是否登录了
        $this->isLogin();

        $id=Request::param('id');
        $cate=CateModel::get($id);
        $cate['status_num']=$cate->getData('status');
        $this->assign('cate',$cate);
        //设置标题
        $this->assign('title','分类编辑');
        //渲染
        return $this->fetch();
    }

    //处理分类编辑
    public function doEdit(){
        if(Request::isPost()){
            $data=Request::post();
            $id=$data['id'];
            unset($data['id']);
            CateModel::where('id',$id)->Update($data);
            $this->success('修改成功','cate/index');
        }
    }

    //删除栏目
    public function deleteCate()
    {
        $id=Request::param('id');
        if(CateModel::destroy($id)){
            $this->success('删除栏目成功','cate/index');
        }else{
            $this->error('删除栏目失败','cate/index');
        }

    }
}